<?php

namespace app\api\controller;

use app\common\controller\WeChat;
use think\facade\Db;

class Cron
{
    public function ceshi()
    {
        $news = Db::name('news')->where('mobile_more',null)->update(['mobile_more'=>'']);
        dd($news);
    }

    /**
     * 统计信息总数
     */
    public function newsTotal()
    {
        // $where[] = ["delete_time","=",0];
        // $where[] = ["news_file_id","=",0];
        // $total = Db::name('news')->where($where)->order('id desc')->count();
        $total = Db::name('news')->order('id desc')->count();
        Db::name('news_total')->where('id',1)->update(['total'=>$total]);
        echo "更新总量完成";
        exit;
    }
    
    
      /**
     * 统计信息总数
     */
    public function updatehasmobile()
    {
        // $where[] = ["delete_time","=",0];
        // $where[] = ["news_file_id","=",0];
        // $total = Db::name('news')->where($where)->order('id desc')->count();
        $total = Db::execute("UPDATE smhs_news SET has_mobile = CASE WHEN mobile IS NOT NULL AND mobile != '' THEN TRUE ELSE FALSE END,is_status_active = (status = '存续'), zhuce_amount = CASE WHEN zhuce LIKE '%万人民币%' THEN CAST(REPLACE(SUBSTRING_INDEX(zhuce, '万人民币', 1), ',', '') AS DECIMAL(15,2)) * 10000 WHEN zhuce LIKE '%万美元%' THEN CAST(REPLACE(SUBSTRING_INDEX(zhuce, '万美元', 1), ',', '') AS DECIMAL(15,2)) * 7.25 * 10000 WHEN zhuce LIKE '%万港币%' THEN CAST(REPLACE(SUBSTRING_INDEX(zhuce, '万港币', 1), ',', '') AS DECIMAL(15,2)) * 0.93 * 10000 WHEN zhuce LIKE '%万新加坡元%' THEN CAST(REPLACE(SUBSTRING_INDEX(zhuce, '万新加坡元', 1), ',', '') AS DECIMAL(15,2)) * 5.3 * 10000 ELSE NULL END WHERE has_mobile=-1 LIMIT 100000;");
        Db::execute("update `smhs_news` set status='存续' , has_mobile=-1 where status='正常' or status='在业' or status='续存'  limit 1000;");
      
        echo "OK";
        exit;
    }
    
    /**
     * 会员到期
     */
    public function memberOut()
    {
        $data = Db::name('user')
                ->where([['is_vip_one|is_vip_two|is_svip_one|is_svip_two','=',1]])
                ->order('id desc')
                ->select()
                ->toArray();
        
        if (!empty($data)) {
            foreach ($data as &$val) {
                if ($val['is_vip_one'] == 1) {
                    if (time() > $val['vip_end_time_one']) {
                        $val['is_vip_one'] = 0;
                        Db::name('user')->where('id',$val['id'])->update(['is_vip_one'=>0,'vip_end_time_one'=>0]);
                    }
                }
                if ($val['is_vip_two'] == 1) {
                    if (time() > $val['vip_end_time_two']) {
                        $val['is_vip_one'] = 0;
                        Db::name('user')->where('id',$val['id'])->update(['is_vip_two'=>0,'vip_end_time_two'=>0]);
                    }
                }
                if ($val['is_svip_one'] == 1) {
                    if (time() > $val['svip_end_time_one']) {
                        $val['is_svip_one'] = 0;
                        Db::name('user')->where('id',$val['id'])->update(['is_svip_one'=>0,'svip_end_time_one'=>0]);
                    }
                }
                if ($val['is_svip_two'] == 1) {
                    if (time() > $val['svip_end_time_two']) {
                        $val['is_svip_one'] = 0;
                        Db::name('user')->where('id',$val['id'])->update(['is_svip_two'=>0,'svip_end_time_two'=>0]);
                    }
                }
            }
        }
        echo 2;exit;
    }


    /**
     * 自动结算订单
     */
    public function jie()
    {
        $data = Db::name('wallet_log')
                ->where(['delete_time'=>0,'status'=>0])
                ->order('id desc')
                ->select()
                ->toArray();
        if (empty($data)) {
            echo 1;exit();
        }
        $config = Db::name('config')->where('id',14)->find();
        $conf = unserialize($config['content']);
        $times = $conf['hours']*3600;
        foreach ($data as &$val) {
            $order = Db::name('order')->where('id',$val['order_id'])->find();
            $pay_time = $val['create_time']+$times;
            if (time() < $pay_time) {
                continue;
            }
            if (empty($order['is_end'])) {
                //累计消费
                Db::name('user')->where('id',$order['uid'])->inc('all_use',floatval($order['amount']))->update();
                dd(123);
                $user = Db::name('user')->where('id',$order['uid'])->find();
                if (!empty($user['level'])) {
                    $user_level_info = Db::name('user_level')->where('id',$user['level'])->find();
                    $score = floor($order['amount']/10);
                    if ($score > 0) {
                        $score_total = $user_level_info['explain']*$score;
                        Db::name('user')->where('id',$order['uid'])->inc('score',intval($score_total))->update();
                        Db::name('score_log')->insert([
                            'uid' => $user['id'],
                            'title' => '购买商品',
                            'num' => intval($score_total),
                            'create_time' => time()
                        ]);
                    }
                }
                $user_level = Db::name('user_level')->where([['id','>',$user['level']]])->order('id asc')->select()->toArray();
                if (!empty($user_level)) {
                    foreach ($user_level as &$v) {
                        if ($user['all_use'] >= $v['money']) {
                            //升级会员
                            Db::name('user_level_log')->insert([
                                'uid' => $order['uid'],
                                'level' => $v['id'],
                                'level_money' => $v['money'],
                                'money' => $user['all_use'],
                                'create_time' => time(),
                            ]);
                            Db::name('user')->where('id',$order['uid'])->update([
                                'level' => $v['id'],
                                'update_time' => time(),
                            ]);
                        }
                    }
                }
            }
            Db::name('wallet_log')->where('id',$val['id'])->update(['status'=>1,'update_time'=>time()]);
            Db::name('order')->where('id',$val['order_id'])->update(['is_end'=>1,'jie_time'=>time()]);
            if ($val['user_type'] == 1) {
                Db::name('user_one')->where('id',$val['uid'])->inc('wallet',floatval($val['num']))->update();
            } elseif ($val['user_type'] == 2) {
                Db::name('user_two')->where('id',$val['uid'])->inc('wallet',floatval($val['num']))->update();
            } else {
                Db::name('user_three')->where('id',$val['uid'])->inc('wallet',floatval($val['num']))->update();
            }
        }
        echo 2;exit();
    }
    
    /**
     * 兑换券过期
     */
    public function couponOut()
    {
        $data  = Db::name('coupon_log')->where(['status'=>1])->order('id desc')->select()->toArray();
        if (empty($data)) {
            echo 1;exit();
        }
        foreach ($data as &$val) {
            if ($val['end_time'] < time()) {
                //券已过期
                Db::name('coupon_log')->where('id',$val['id'])->update(['status'=>3,'update_time'=>time()]);
            }
        }
        echo 2;exit();
    }
}